ExcelTool = function()
{
	this.typeAsset = 
	{
		diemcapphep: 1,
		diemvipham: 2
	};
	this.arrayAsset = [];
	this.setImgDCP = function(idForm, idFileInput, idDivPreview)
	{
		var formImg = $(idForm)[0];
		formImg.method = "post";
		formImg.enctype="multipart/form-data";
		formImg.action= sPortletJSP + "excel/upload_excel_diemcapphep.jsp?server=" + serverName + "&portlet_jsp=" + sPortletJSP;
		var inputTextAlias = $(idFileInput)[0];
		inputTextAlias.type = 'file';
		//inputTextAlias.accept = "image/*";			
		var divPreview = $(idDivPreview)[0];		
		$(inputTextAlias).live('change', function () {
	    	var sPortlet = sPortletImg + "loader.gif";
	        $(divPreview).html('');
	        $(divPreview).html('<img src="' + sPortlet + '" alt="Uploading...."//>');
	        //$(divPreview).hide();
	        $(formImg).ajaxForm({
	            target: idDivPreview
	        }).submit(
    		);
	    });
		$(inputTextAlias).change(function () {
		}).change();
	};
	
	this.setImgDVP = function(idForm, idFileInput, idDivPreview)
	{
		var formImg = $(idForm)[0];
		formImg.method = "post";
		formImg.enctype="multipart/form-data";
		formImg.action= sPortletJSP + "excel/upload_excel_diemvipham.jsp?server=" + serverName + "&portlet_jsp=" + sPortletJSP;
		var inputTextAlias = $(idFileInput)[0];
		inputTextAlias.type = 'file';
		//inputTextAlias.accept = "image/*";			
		var divPreview = $(idDivPreview)[0];		
		$(inputTextAlias).live('change', function () {
	    	var sPortlet = sPortletImg + "loader.gif";
	        $(divPreview).html('');
	        $(divPreview).html('<img src="' + sPortlet + '" alt="Uploading...."//>');
	        $(formImg).ajaxForm({
	            target: idDivPreview
	        }).submit(
    		);
	    });
		$(inputTextAlias).change(function () {
		}).change();
	};
	
	this.setImgCGDD = function(idForm, idFileInput, idDivPreview)
	{
		var formImg = $(idForm)[0];
		formImg.method = "post";
		formImg.enctype="multipart/form-data";
		formImg.action= sPortletJSP + "excel/upload_excel_chigioduongdo.jsp?server=" + serverName + "&portlet_jsp=" + sPortletJSP;
		var inputTextAlias = $(idFileInput)[0];
		inputTextAlias.type = 'file';	
		$(inputTextAlias).live('change', function () {
	    });
		$(inputTextAlias).change(function () {
		}).change();
	};
	
	this.setImgCGXD = function(idForm, idFileInput, idDivPreview)
	{
		var formImg = $(idForm)[0];
		formImg.method = "post";
		formImg.enctype="multipart/form-data";
		formImg.action= sPortletJSP + "excel/upload_excel_chigioxaydung.jsp?server=" + serverName + "&portlet_jsp=" + sPortletJSP;
		var inputTextAlias = $(idFileInput)[0];
		inputTextAlias.type = 'file';	
		$(inputTextAlias).live('change', function () {
	    });
		$(inputTextAlias).change(function () {
		}).change();
	};
	
	this.importExcel = function(idForm, idDivPreview) {
		disableMainPage();
    	var sPortlet = sPortletImg + "loader.gif";
		var divPreview = $(idDivPreview)[0];
		var formImg = $(idForm)[0];
        $(divPreview).html('');
        $(divPreview).html('<img src="' + sPortlet + '" alt="Uploading...."//>');
        $(divPreview).hide();
        $(formImg).ajaxForm({
            target: idDivPreview,
            success: function(msg){
            	msg = base.trim(msg.replace(/(\r\n|\n|\r)/gm, ""));
            	$(idDivPreview).text(msg);
                enableMainPage();
                if(msg.indexOf('1') != -1) alert(LANGUAGE['import_excel_success']);
                else alert(LANGUAGE['import_excel_failed']);
	          },
	          error: function(XMLHttpRequest, textStatus, errorThrown) {
	        	  alert(LANGUAGE['import_excel_failed']);
	        	  MapAction.RefreshAction();
                  enableMainPage();
	          }
        }).submit();
	};
	
	this.import = function(typeAsset, layerId)
	{		 	
	 	// lay index layer
		var vector = LayerTool.getVectorLayer(layerId);
		var indexLayer = vector.IndexLayer;
		var layerAsset = map.layers[indexLayer];
		var arrData = this.arrayAsset;
		var i = 0;
		var objExcel = null;
		var attrNames = null;
		var features = [];
		for(i=0; i<arrData.length; i++)
		{
			objExcel = arrData[i];
			var featuresExits = null;
			attrNames =  objExcel.attrNames;
			if(typeAsset == this.typeAsset.diemcapphep)
			{
				var sogiayphepID = attrNames["sogiayphep"];
				featuresExits = layerAsset.getFeaturesByAttribute("sogiayphep", sogiayphepID);
			}
			if(typeAsset == this.typeAsset.diemvipham)
			{
				var mavipham = attrNames["mavipham"];
				featuresExits = layerAsset.getFeaturesByAttribute("mavipham", mavipham);
			}
			
			var pPoint = new OpenLayers.Geometry.Point();
			pPoint.x = parseFloat(attrNames["x"]);
			pPoint.y = parseFloat(attrNames["y"]);			
			var pointFeature = new OpenLayers.Feature.Vector(pPoint, attrNames);
			if(featuresExits.length == 0) 
			{
				pointFeature.state = OpenLayers.State.INSERT;
				features[features.length] = pointFeature;
			}
			else 
			{
				pointFeature = featuresExits[0];
				pointFeature.geometry = pPoint;
				pointFeature.attributes = attrNames;
				pointFeature.state = OpenLayers.State.UPDATE;
			}	
		}
		layerAsset.addFeatures(features);
		if(saveObj[indexLayer]!=null) saveObj[indexLayer].save();
		MapAction.RefreshAction();
		
		alert(LANGUAGE["msg_import_excel_success"]);
		FLAG_COMPLETETED = false;
	};
	
    this.loadExcel = function()
    {
        $.ajax({
            type: "POST",
            url: "/AnGiang-557-portlet/html/giamsatmoitruong/SHP/loadSHP.jsp",
            data: "dataString",
            success: function (data) {
                var result = base.trim(data.replace(/(\r\n|\n|\r)/gm, ""));        	
                if(result == "0")
                {
                    alert("0");
                    return;
                }
                $('#divPreview_Map_BandoquyhoachChung_ImageUpload_Action').empty();
                $('#divPreview_Map_BandoquyhoachChitiet_ImageUpload_Action').empty();
            	var indexLayer = LayerTool.getIndexLayer();
            	var layerObj = map.layers[indexLayer];
                var arrFeatures = result.split('&');
                var i=0, j=0, k=0;
                for(k=0;k<arrFeatures.length-1;k++)
                {
    	           // create some attributes for the feature
    	           var attributes = new Object();
                	          
                    var arrItems = arrFeatures[k].split('|');
                    var arrAttrs = arrItems[0];
                    var arrCoors = arrItems[1];
                    // lay data
                    var _arrAttrs = arrAttrs.split(';');
                    for(i=0;i<_arrAttrs.length-1;i++)
                    {
                        var __arrAtts = _arrAttrs[i].split(':');
                        // ten truong
                        var sFieldName = __arrAtts[0];
                        // du lieu
                        var sData = __arrAtts[1];
                        attributes[sFieldName] = sData;
                    }
                    // lay coor

                    var point = null;
                	var points = [];
    	           
                    var _arrCoors = arrCoors.split(';');
                    for(j=0;j<_arrCoors.length-1;j++)
                    {
                        var __arrCoors = _arrCoors[j].split(',');
                        var x = __arrCoors[0];
                        var y = __arrCoors[1];
                        point = new OpenLayers.Geometry.Point(x, y);
                        points[points.length] = point;
                    }
    	           var ring = new OpenLayers.Geometry.LinearRing(points);
    	           var polygon = new OpenLayers.Geometry.Polygon([ring]);
    	           var multyPolygon = new OpenLayers.Geometry.MultiPolygon(polygon);
     	           var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.MultiPolygon(), attributes);
     	           feature.state = OpenLayers.State.INSERT;
     	           layerObj.addFeatures([feature]);
                }
                layerObj.redraw(true);
                return;
            }
        });
    };
};
var excelTool = new ExcelTool();